<template>
  <div class="water">
    <!-- 水质测试 -->
    <div class="water_test">
      <div class="name">水质测试</div>
      <div class="reportBox">
        <div class="report">
          <div class="report_t">{{ detailInfo.before_water_quality }}</div>
          <div class="report_b">
            净化前水质
            <div class="level">一般</div>
          </div>
        </div>
        <div class="report">
          <div class="report_t">{{ detailInfo.after_water_quality }}</div>
          <div class="report_b">
            净化后水质
            <div class="level level_b">优</div>
          </div>
        </div>
      </div>
    </div>
    <div class="waterConsumption">
      <div class="waterConsumption_top">
        <div class="waterConsumption_top_l">
          <div class="name">用水量</div>
          <div class="dayBox">
            <div class="today" style="margin-right: 0.31rem;">
              <div class="txt">今日</div>
              <div class="txt">{{ waterLog.today_water || "--" }}L</div>
            </div>
            <div class="today">
              <div class="txt">昨日</div>
              <div class="txt">{{ waterLog.yesterday_water || "--" }}L</div>
            </div>
          </div>
        </div>
        <div class="waterConsumption_top_r" @click="toRecord">
          历史用水情况
          <img src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FdrinkMachind%2Ficon_more1%402x.png" alt="" />
        </div>
      </div>
      <div id="echats-income" style="height: 260px; width: 21rem;"></div>
      <div class="datSelect">
        <img @click="sub" src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FdrinkMachind%2Ficon_left%402x.png" alt="" />
        <div class="day">{{ dateStr }}</div>
        <img @click="add" src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FdrinkMachind%2Ficon_right@2x.png" alt="" />
      </div>
    </div>
    <div class="waterConsumption mountings">
      <div class="waterConsumption_top">
        <div class="waterConsumption_top_l">
          <div class="name">配件寿命</div>
          <div class="name_de">配件寿命按照用水量和使用时间综合推移</div>
        </div>
        <div class="waterConsumption_top_r" @click="refreshMethod">
          刷新
          <img src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FdrinkMachind%2Ficon_update@2x.png" alt="" />
        </div>
      </div>
      <div class="mountings_b">
        <div class="mountings_b_line" v-for="(item, index) in detailInfo.part_data" :key="index">
          <div class="txt">{{ item.name }}</div>
          <div class="centenBox">
            <div class="centenBox_txt" v-show="item.show_rest_day">预计剩余寿命：{{ item.rest_day }}天</div>
            <div class="centenBox_line">
              <div class="centenBox_line_pos" :style="{ width: item.percent + '%' }"></div>
            </div>
          </div>
          <div class="ratio" v-show="item.show_percent">{{ item.percent }}%</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import echarts from "utils/echarts";
export default {
  props: ["detailInfo", "machineId"],
  data() {
    return {
      dateStr: "",
      waterLog: [],
      oneDay: 1000 * 60 * 60 * 24
    };
  },
  mounted() {
    let nowt = new Date();
    this.dateStr = `${nowt.getFullYear()}-${nowt.getMonth() + 1 < 10 ? "0" + (nowt.getMonth() + 1) : nowt.getMonth() + 1}-${nowt.getDate()}`;
    this.waterRecord();
  },
  methods: {
    toRecord() {
      this.$router.push(
        this.fun.getUrl("drinkMachindRecord", {
          id: this.machineId
        })
      );
    },
    waterRecord() {
      let daystr = this.dateStr.replace(/-/g, "");
      console.log(daystr);
      $http.post("plugin.drink-machine.frontend.machine.waterLog", { machine_id: this.machineId, day: daystr }, "加载中...").then(res => {
        if (res.result == 1) {
          this.waterLog = res.data;
          this.initEcharts();
        } else {
          this.$toast(res.msg);
        }
      });
    },
    add() {
      let beforeDay = Date.parse(this.dateStr) + this.oneDay;
      let nowt = new Date(beforeDay);
      this.dateStr = `${nowt.getFullYear()}-${nowt.getMonth() + 1 < 10 ? "0" + (nowt.getMonth() + 1) : nowt.getMonth() + 1}-${nowt.getDate() < 10 ? "0"+nowt.getDate():nowt.getDate()}`;
      this.waterRecord();
    },
    sub() {
      let beforeDay = Date.parse(this.dateStr) - this.oneDay;
      let nowt = new Date(beforeDay);
      this.dateStr = `${nowt.getFullYear()}-${nowt.getMonth() + 1 < 10 ? "0" + (nowt.getMonth() + 1) : nowt.getMonth() + 1}-${nowt.getDate() < 10 ? "0"+nowt.getDate():nowt.getDate()}`;
      this.waterRecord();
    },
    refreshMethod() {
      this.$emit("refresh");
    },
    initEcharts(key) {
      let $echartsDOM = document.getElementById("echats-income");
      let myEcharts = echarts.init($echartsDOM);
      let maxMoney = Math.ceil(Math.max(...this.waterLog.water_list)) || 50;
      let intervalMoney = maxMoney / 5 || 5;
      let option = {
        tooltip: {
          trigger: "axis"
        },
        toolbox: {
          feature: {
            magicType: { show: true, type: ["bar"] },
            saveAsImage: { show: true }
          }
        },
        legend: {
          data: ["单次用水量"],
          itemStyle: {
            barBorderRadius: [12]
          },
          x: "center",
          y: "bottom"
        },
        xAxis: [
          {
            type: "category",
            data: this.waterLog.day_list,
            axisTick: {
              // 坐标轴刻度相关设置
              show: false //X轴默认是显示的
            },
            axisLabel: {
              interval: 0
            }
          }
        ],
        yAxis: [
          {
            type: "value",
            name: "单次用水量",
            min: 0,
            max: maxMoney,
            interval: intervalMoney
          }
        ],
        series: [
          {
            name: "单次用水量",
            type: "bar",
            color: "#015EF5",
            data: this.waterLog.water_list,
            itemStyle: {
              barBorderRadius: [9, 9, 0, 0],
              barwidth: "20%"
            },
            barCategoryGap: "30%"
          }
        ]
      };
      myEcharts.setOption(option);
    }
  }
};
</script>
<style lang="scss" scoped>
.water {
  margin: 0 0.75rem;
  padding-bottom: 2rem;

  .mountings {
    margin-top: 0.5rem;

    .mountings_b {
      margin-top: 1.56rem;
      display: flex;
      flex-direction: column;

      .mountings_b_line {
        display: flex;
        justify-content: space-between;
        margin-bottom: 1.47rem;
        align-items: center;

        .txt {
          font-weight: bold;
          font-size: 0.88rem;
          flex-shrink: 0;
          width: 18%;
          text-align: left;
        }

        .centenBox {
          flex: 1;
          display: flex;
          flex-direction: column;
          padding-left: 0.53rem;
          box-sizing: border-box;

          .centenBox_txt {
            font-size: 0.63rem;
            font-weight: 400;
            text-align: left;
          }

          .centenBox_line {
            width: 13.13rem;
            border-radius: 0.94rem;
            background: #d6d6dc;
            height: 0.31rem;
            margin-top: 0.28rem;
            position: relative;

            .centenBox_line_pos {
              position: absolute;
              left: 0;
              top: 0;
              width: 100%;
              height: 100%;
              background: #015ef5;
              border-radius: 0.94rem;
            }
          }
        }

        .ratio {
          width: 15%;
          flex-shrink: 0;
          margin: 0 0.53rem;
        }
      }
    }
  }

  .waterConsumption {
    padding: 0.88rem 0.75rem;
    background: #fff;
    border-radius: 0.75rem;

    .datSelect {
      display: flex;
      align-items: center;
      justify-content: center;
      margin-top: 1rem;

      img {
        width: 1rem;
        height: 1rem;
        margin: 0;
      }

      .day {
        font-size: 0.88rem;
        font-weight: 500;
        margin: 0 0.81rem;
      }
    }

    .waterConsumption_top {
      display: flex;
      justify-content: space-between;
      align-items: flex-start;

      .waterConsumption_top_r {
        display: flex;
        font-size: 0.75rem;
        font-weight: 400;
        color: #015ef5;
        padding: 0.5rem 0.94rem;
        box-sizing: border-box;
        background: #e5eefe;
        border-radius: 0.8rem;

        img {
          width: 1rem;
          height: 1rem;
          margin: 0;
        }
      }

      .waterConsumption_top_l {
        display: flex;
        flex-direction: column;

        .name {
          font-size: 0.94rem;
          font-weight: bold;
          text-align: left;
        }

        .name_de {
          font-size: 0.69rem;
          color: #aaaab3;
          margin-top: 0.4rem;
        }

        .dayBox {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          margin-top: 0.63rem;

          .today {
            min-width: 4.25rem;
            padding: 0.38rem 0.53rem;
            border-radius: 0.5rem;
            border: 0.03rem solid #d6d6dc;

            .txt {
              font-size: 0.75rem;
              font-weight: 400;
              color: #00001c;
            }
          }
        }
      }
    }
  }

  .water_test {
    background: #fff;
    border-radius: 0.75rem;
    padding: 0.9rem 0.75rem;
    margin-bottom: 0.5rem;

    .name {
      font-weight: bold;
      color: #00001c;
      font-size: 0.94rem;
      text-align: left;
    }

    .reportBox {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .report {
        margin-top: 1.25rem;
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        width: 50%;

        .report_t {
          font-size: 1rem;
          font-weight: bold;
          color: #00001c;
        }

        .report_b {
          font-size: 0.75rem;
          font-weight: 400;
          color: #6e6e79;
          display: flex;
          align-items: center;

          .level {
            margin-left: 0.31rem;
            font-size: 0.63rem;
            padding: 0.22rem 0.38rem;
            background: #f0f0f1;
            border-radius: 0.94rem;
          }

          .level_b {
            min-width: 2rem;
            color: #1ac970;
          }
        }
      }
    }
  }
}
</style>
